//
//  PopularActivitiesVC.m
//  massage
//
//  Created by 李志权 on 2017/1/19.
//  Copyright © 2017年 李志权. All rights reserved.
//

#import "PopularActivitiesVC.h"
#import "MyWebViewVC.h"
@interface PopularActivitiesVC ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIView *greenLien;//绿线
    UIScrollView *_scrollView;
    NSMutableArray *activityInfoData;//活动消息
    NSMutableArray *merchantsData;//电脑平台
    NSMutableArray *storesInfoData;//门店信息
}
@end

@implementation PopularActivitiesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"热门活动";
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 40)];
    aView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aView];
    UIView *lien = [[UIView alloc]initWithFrame:CGRectMake(0, aView.highly-1, Width, 1)];
    lien.backgroundColor = GrayTooLight;
    [aView addSubview:lien];
    
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, aView.YHighly, Width, Highly-aView.YHighly)];
    _scrollView.backgroundColor = [UIColor purpleColor];
    _scrollView.contentSize = CGSizeMake(Width*3, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    activityInfoData = [NSMutableArray array];
    merchantsData = [NSMutableArray array];
    storesInfoData = [NSMutableArray array];
    
    NSArray *array = @[@"活动信息",@"电商平台",@"门店信息"];
    for (int a = 0; a<array.count; a++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(a*(Width/3), 0, Width/3, aView.highly-2);
        [btn setTitleColor:GreenColor forState:UIControlStateSelected];
        [btn setTitleColor:MAINBLUEGrayDark forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitle:array[a] forState:UIControlStateNormal];
        btn.tag = 700+a;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [aView addSubview:btn];
        
        if (a == 0) {
            btn.selected = YES;
        }
        
        UITableView *tableView =[[UITableView alloc]initWithFrame:CGRectMake(Width*a, 0, _scrollView.width, _scrollView.highly)];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = NO;
        tableView.tag = 700+a;
        [_scrollView addSubview:tableView];
        
        [Utils systemPOSTMethod:appUrl_userGetItemInfo Parameters:[NSString stringWithFormat:@"itemId=%d",a+1] success:^(NSURLSessionDataTask *task, id responseObject) {
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSArray *jsonArray = [Utils parseJSON:str];
            for (NSDictionary *dic in jsonArray) {
                Model *model = [[Model alloc]initWithModel:dic];
                if (a == 0)
                    [activityInfoData addObject:model];
                else if (a==1)
                    [merchantsData addObject:model];
                else if (a==2)
                    [storesInfoData addObject:model];
            }
            [tableView reloadData];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
    greenLien = [[UIView alloc]initWithFrame:CGRectMake(0, aView.highly-2, Width/3, 2)];
    greenLien.backgroundColor = GreenColor;
    [aView addSubview:greenLien];
    
    
    

   

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)btnClick:(UIButton *)btn
{
    
    [UIView animateWithDuration:0.25 animations:^{
        greenLien.X = btn.X;
    }];
    [UIView animateWithDuration:0.25 animations:^{
        _scrollView.contentOffset = CGPointMake(Width*(btn.tag-700), 0);
    }];
    int index = _scrollView.contentOffset.x/_scrollView.width;
    for (int a = 0; a<3; a++) {
        UIButton *btn = [self.view viewWithTag:700+a];
        btn.selected = a==index ? YES:NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat index = scrollView.contentOffset.x/scrollView.width;
    [UIView animateWithDuration:0.25 animations:^{
        greenLien.X = index*greenLien.width;
    }];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x/scrollView.width;
    for (int a = 0; a<3; a++) {
        UIButton *btn = [self.view viewWithTag:700+a];
        btn.selected = a==index ? YES:NO;
    }
}
- (NSMutableArray *)getData:(UITableView *)tableView
{
    switch (tableView.tag) {
        case 700:
            if (activityInfoData && activityInfoData.count) {
                return activityInfoData;
            }
            break;
        case 701:
            if (merchantsData && merchantsData.count) {
                return merchantsData;
            }
            break;
        case 702:
            if (storesInfoData && storesInfoData.count) {
                return storesInfoData;
            }
            break;
        default:
            break;
    }
    return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *data = [self getData:tableView];
    if (data && data.count) {
        return data.count;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"myCell";
    tableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[tableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    NSMutableArray *data = [self getData:tableView];
    if (data && data.count>indexPath.row) {
        [cell setModelFrame:data[indexPath.row]];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *data = [self getData:tableView];
    Model *model = data[indexPath.row];
    return model.cellHighly;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *data = [self getData:tableView];
    Model *model = data[indexPath.row];
    activityInfoVC *VC = [[activityInfoVC alloc]init];
    VC.content = model.data[@"content"];
    [self.navigationController pushViewController:VC animated:YES];
}
@end

@implementation Model

- (id)initWithModel:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        
        [[[UIImageView alloc]init] sd_setImageWithURL:[NSURL URLWithString:data[@"picture"]] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
//            self.image = [ImageCompress fixToFillScreenWithImage:image width:Width-40];
            
        }];
        self.imageURL = data[@"picture"];
        self.pictureFrame = CGRectMake(20, 15, Width-40, 160);
        self.title = data[@"title"];
        NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGSize size = [self.title boundingRectWithSize:CGSizeMake(Width-40,MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  attributes:attribute context:nil].size;
        
        self.titleLabelFrame = CGRectMake(20, self.pictureFrame.origin.y+self.pictureFrame.size.height+10, Width-40, size.height);
        
        self.cellHighly = self.titleLabelFrame.origin.y+self.titleLabelFrame.size.height+5;
        self.data = data;
        
    }
    return self;
}

@end

@implementation tableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.picture = [[SDNetImageView alloc]init];
//        self.picture.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.picture];
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textColor = GrayLight;
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}
- (void)setModelFrame:(Model *)model
{
    self.picture.frame = model.pictureFrame;
    
    self.picture.imageURL = model.imageURL;
    self.titleLabel.frame = model.titleLabelFrame;
    self.titleLabel.text = model.title;
}
@end

@implementation activityInfoVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   UIWebView *web= [[UIWebView alloc]initWithFrame:self.view.bounds];
    [web loadHTMLString:self.content baseURL:nil];
    [self.view addSubview:web];
    
}

@end
